﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using HCFramework.API.LINQ;
using System.IO;

namespace HCFramework.AdminTool.Email
{
    public partial class SendEmail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Load default data
                txtTo.Text = Session["EmailTo"]+"";
                txtSubject.Text = Session["EmailSubject"]+"";
                RadEditor1.Content = Session["EmailContent"]+"";
                // Feedback
                if (M.UrlQuery.feedBackID.HasValue) LoadFeedbackData(M.UrlQuery.feedBackID.Value);
                // User
                if (M.UrlQuery.userID.HasValue) LoadUserData(M.UrlQuery.userID.Value);
                // Order
                if (M.UrlQuery.orderID.HasValue) LoadOrderData(M.UrlQuery.orderID.Value);
            }
        }

        private void LoadOrderData(int orderID)
        {
            Order order = C.OrderController.GetByID(orderID);
            OrderContact oc = C.OrderContactController.GetOrInsert(orderID);
            txtTo.Text = oc.Email;
            string content = M.FileM.Read(@"\AdminTool\Email\EmailTemplate\SendToOrderContact.htm");
            content = M.Template.MergeOrder(content, order);
            content = M.Template.MergeOrderContact(content, oc);
            RadEditor1.Content = content;
        }

        private void LoadUserData(int userID)
        {
            API.LINQ.User user = C.UserController.GetByID(userID);
            txtTo.Text = user.Email;
            string content = File.ReadAllText(Server.MapPath(@"EmailTemplate\SendToUser.htm"));
            content = content.Replace("{Name}", user.FirstName + " " + user.LastName);
            content = content.Replace("{Domain}", M.Common.GetRootAddress());
            RadEditor1.Content = content;
        }

        private void LoadFeedbackData(int feedbackID)
        {
            API.LINQ.FeedBack feedBack = C.FeedBackController.GetByID(feedbackID);
            txtTo.Text = feedBack.Email;
            txtSubject.Text = feedBack.Subject;
            string content = File.ReadAllText(Server.MapPath(@"EmailTemplate\FeedbackReply.htm"));
            content = content.Replace("{oldContent}", feedBack.Content);
            content = content.Replace("{name}", feedBack.Name);
            content = content.Replace("{currentDomain}", M.Common.GetRootAddress());
            RadEditor1.Content = content;
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            // Gửi email
            MailMessage m = new MailMessage();
            m.To.Add(txtTo.Text);
            m.Subject = txtSubject.Text;
            m.Body = RadEditor1.Content;
            M.Common.SendEmail(m);
            Message1.ShowSuccess("Email sent");
            // Nếu là gửi email feedback thì cập nhật trạng thái là đã gửi
            if (M.UrlQuery.feedBackID.HasValue)
            {
                HCDataContext db = new HCDataContext();
                API.LINQ.FeedBack f = C.FeedBackController.GetByID(M.UrlQuery.feedBackID.Value, db);
                f.IsReply = true;
                C.FeedBackController.Update(f, db);
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtTo.Text = "";
            txtSubject.Text = "";
            RadEditor1.Content = "";
        }
    }
}